package com.pig4cloud.pig.module.service.product.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.module.api.entity.mer.Mer;
import com.pig4cloud.pig.module.api.entity.product.Product;
import com.pig4cloud.pig.module.api.entity.product.ProductType;
import com.pig4cloud.pig.module.api.vo.TreeSelect;
import com.pig4cloud.pig.module.mapper.mer.MerMapper;
import com.pig4cloud.pig.module.mapper.product.ProductMapper;
import com.pig4cloud.pig.module.mapper.product.ProductTypeMapper;
import com.pig4cloud.pig.module.service.product.IProductTypeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {


	@Resource
	private ProductMapper productMapper;
	@Resource
	private MerMapper merMapper;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

	/**
	 * 查询部门管理数据
	 *
	 * @param type 部门信息
	 * @return 部门信息集合
	 */
	@Override
	public List<ProductType> selectProductTypeList(ProductType type) {
		QueryWrapper<ProductType> qw = new QueryWrapper<>();
		qw.eq("state",0);
		if(type.getParentId()!=null) {
			qw.eq("parent_id",type.getParentId());
		}
		if(type.getMerId()!=null) {
			qw.eq("mer_id",type.getMerId());
		}
		qw.orderByAsc("sort");
		List<ProductType> list = baseMapper.selectList(qw);
		list.forEach(productType -> {
			QueryWrapper<Product> qwProduct =new QueryWrapper<>();
			if(productType.getParentId()!=null) {
				qwProduct.eq("cate_child_id",productType.getId());
			}else{
				qwProduct.eq("cate_id",productType.getId());
			}
			qwProduct.eq("is_show",1);
			long productCount = productMapper.selectCount(qwProduct);
			productType.setProductCount(productCount);

			QueryWrapper<Mer> qwMer =new QueryWrapper<>();
			if(productType.getParentId() !=null && productType.getParentId() > 0) {
				qwMer.eq("cate_child_id",productType.getId());
			}else{
				qwMer.eq("cate_id",productType.getParentId());
			}
			qwMer.eq("state",0);
			long merCount = merMapper.selectCount(qwMer);
			productType.setMerCount(merCount);


			if(StrUtil.isNotEmpty(productType.getImgUrl())){
				if(StrUtil.isNotEmpty(productType.getImgUrl()) && productType.getImgUrl().indexOf("http") < 0){
					productType.setImgPath(imgUrl + productType.getImgUrl());
				}else{
					productType.setImgPath(productType.getImgUrl());
				}
			}else{
				productType.setImgPath("");
			}
		});
		return list;
	}

	/**
	 * 构建前端所需要下拉树结构
	 *
	 * @param depts 部门列表
	 * @return 下拉树结构列表
	 */
	@Override
	public List<TreeSelect> buildProductTypeTreeSelect(List<ProductType> depts) {
		List<ProductType> deptTrees = buildProductTypeTree(depts);
		return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
	}


	/**
	 * 构建前端所需要树结构
	 *
	 * @param depts 部门列表
	 * @return 树结构列表
	 */
	@Override
	public List<ProductType> buildProductTypeTree(List<ProductType> depts) {
		List<ProductType> returnList = new ArrayList<>();
		List<Integer> tempList = new ArrayList<>();
		for (ProductType dept : depts) {
			tempList.add(dept.getId());
		}
		for (ProductType dept : depts) {
			// 如果是顶级节点, 遍历该父节点的所有子节点
			if (!tempList.contains(dept.getParentId())) {
				recursionFn(depts, dept);
				returnList.add(dept);
			}
		}
		if (returnList.isEmpty()) {
			returnList = depts;
		}
		return returnList;
	}

	/**
	 * 递归列表
	 */
	private void recursionFn(List<ProductType> list, ProductType t) {
		// 得到子节点列表
		List<ProductType> childList = getChildList(list, t);
		t.setChildren(childList);
		for (ProductType tChild : childList) {
			if (hasChild(list, tChild)) {
				recursionFn(list, tChild);
			}
		}
	}


	/**
	 * 得到子节点列表
	 */
	private List<ProductType> getChildList(List<ProductType> list, ProductType t) {
		List<ProductType> tlist = new ArrayList<>();
		for (ProductType n : list) {
			if (Validator.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue()) {
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<ProductType> list, ProductType t) {
		return getChildList(list, t).size() > 0;
	}
}
